#include <cstdio>
#include <cstring>

#include <algorithm>
#include <map>
#include <vector>
#include <string>

using namespace std;

int n;
vector<int> T1[1005];
vector<int> T2[1005];

map<pair<int, int>, string> memo;

string getMin_T1(int x, int xf)
{
	string res;
	vector<string> v;

	for( vector<int>::iterator it = T1[x].begin(); it != T1[x].end(); ++it )
	{
		if( *it != xf ) v.push_back(getMin_T1(*it, x));
	}
	sort(v.begin(), v.end());

	res = "(";
	for( vector<string>::iterator it = v.begin(); it != v.end(); ++it )
		res += *it;
	res += ")";

	return res;
}

string getMin_T2(int x, int xf)
{
	if( memo.find(make_pair(x, xf)) != memo.end() )
		return memo[make_pair(x, xf)];

	string res;
	vector<string> v;

	for( vector<int>::iterator it = T2[x].begin(); it != T2[x].end(); ++it )
	{
		if( *it != xf ) v.push_back(getMin_T2(*it, x));
	}
	sort(v.begin(), v.end());

	res = "(";
	for( vector<string>::iterator it = v.begin(); it != v.end(); ++it )
		res += *it;
	res += ")";

	return memo[make_pair(x, xf)] = res;
}

int main( void )
{
//	freopen( "input.txt", "r", stdin );

	int nprob; scanf( "%d", &nprob );

	for( ; nprob; --nprob ) {
		scanf( "%d", &n );

		for( int i = 0; i < n; ++i ) {
			T1[i].clear();
			T2[i].clear();
		}
		for( int i = 1; i < n; ++i ) {
			int a, b; scanf( "%d %d", &a, &b );
			--a, --b;
			T1[a].push_back(b);
			T1[b].push_back(a);
		}
		for( int i = 1; i < n; ++i ) {
			int a, b; scanf( "%d %d", &a, &b );
			--a, --b;
			T2[a].push_back(b);
			T2[b].push_back(a);
		}

		string minT1 = getMin_T1(0, -1);
		bool same = false;

		memo.clear();

		for( int i = 0; i < n; ++i ) {
			string minT2 = getMin_T2(i, -1);

			if( minT1 == minT2 ) {same = true; break;}
		}

		if( same ) printf("same\n");
		else printf("different\n");
	}

	return 0;
}
